//
//  MyPalette.m
//  ElectronicSignatureDemo
//
//  Created by Eric on 13-5-16.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "MyPalette.h"

@implementation MyPalette


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _lineArray = [[NSMutableArray alloc] initWithCapacity:10];
        self.backgroundColor = [UIColor clearColor];
       
      
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
       [self draw];
}



-(void)draw
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 7);
    
    //设置画线的连接处　拐点圆滑
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);

    for(int i = 0;i < _lineArray.count;i++)
    { 
        for(int j = 1;j < [[_lineArray objectAtIndex:i] count]-1;j++)
        {
            CGContextSetStrokeColorWithColor(context, [[[_lineArray objectAtIndex:i] objectAtIndex:0] CGColor]);
            
            //绘制画笔的颜色
            //CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
            CGPoint prePoint = [[[_lineArray objectAtIndex:i] objectAtIndex:j] CGPointValue];
            CGPoint curPoint = [[[_lineArray objectAtIndex:i] objectAtIndex:j+1] CGPointValue];
            CGContextMoveToPoint(context, prePoint.x, prePoint.y);
            CGContextAddLineToPoint(context, curPoint.x, curPoint.y);
            
        }
        CGContextStrokePath(context);//把划线的路径显示到上下文里
    }
    
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _pointArray = [[NSMutableArray alloc] initWithCapacity:10];
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    [_pointArray addObject:[UIColor blackColor]];
    [_pointArray addObject:[NSValue valueWithCGPoint:point]];
    
    [_lineArray addObject:_pointArray];
    //_pointArray=nil;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [_pointArray addObject:[NSValue valueWithCGPoint:[touch locationInView:self]]];
    [self setNeedsDisplay];
}
-(void)shanchu
{
    [_lineArray removeLastObject];
    [self setNeedsDisplay];
}
@end
